const asyncHandler = require("../utils/asyncHandler");
const Post = require("../models/Post");
const { success, error } = require('../utils/response')

// 简单统计所有标签及使用次数
exports.listTags = asyncHandler(async (req, res) => {
    const agg = await Post.aggregate([
        { $unwind: "$tags" },
        { $group: { _id: "$tags", count: { $sum: 1 } } },
        { $project: { tag: "$_id", count: 1, _id: 0 } },
        { $sort: { count: -1, tag: 1 } },
    ]);
    return success(res, { items: agg })
});
